package com.kordar.datalist.formatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CreateTimeFormatter implements Formatter<String> {
    @Override
    public String format(Object value, Object... params) {
        if (value == null || value.equals("")) {
            return "";
        }

        String pattern = params[0] == null || "".equals(params[0]) ? "yyyy-MM-dd HH:mm:ss" : (String) params[0];
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if (value.getClass() == String.class) {
            Date date = null;
            try {
                date = sdf.parse((String) value);
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
            return sdf.format(date);
        } else if(value.getClass() == Date.class) {
            Date date = (Date) value;
            return sdf.format(date);
        } else {
            return "";
        }
    }
}
